package com.example.nfcaccesssystem;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.ListView;

public class EditUserActivity extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    setContentView(R.layout.activity_edit_user);
	    
	    Intent i = getIntent();
	    
	    final User u = (User) i.getSerializableExtra("user");
	    
	    ListView doorAccessListView = (ListView)findViewById(R.id.doorAccessListView);
	    
	    ArrayAdapter<Door> adapter = new ArrayAdapter<Door>(this, android.R.layout.simple_list_item_multiple_choice, Controller.getInstance().doorList);
	    
		doorAccessListView.setAdapter(adapter);
	    
	    Controller.getInstance().getDoorsAndAccess(u, this);
	    
	    EditText userNameText = (EditText)findViewById(R.id.userNameText);
	    
	    userNameText.setText(u.getName());
	    
	    EditText userAccessIdText = (EditText)findViewById(R.id.userAccessIdText);
	    
	    userAccessIdText.setText(u.getAccessId() + "");
	    
	    doorAccessListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
	    
	    doorAccessListView.setOnItemClickListener(new OnItemClickListener() {
	    	
	    	@Override
	    	public void onItemClick(AdapterView<?> adapter, View view, int position, long arg){
	    		Door d = (Door) adapter.getItemAtPosition(position);
	    		
	    		CheckedTextView checkedView = (CheckedTextView)view;
	    		
	    		boolean checked = checkedView.isChecked();
	    		
	    		if(checked){
	    			
	    			Controller.getInstance().addUserDoorAccess(u, d);
	    		}

	    		else {
	    				
	    			Controller.getInstance().removeUserDoorAccess(u, d);
	    		}
	    	}
		});
	    
	    Button okNameButton = (Button)findViewById(R.id.editUserNameOkButton);
	    Button okAccesIdButton = (Button)findViewById(R.id.editUserAccessIdOkButton);
	    
	    okNameButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				EditText userNameText = (EditText)findViewById(R.id.userNameText);
				
				Controller.getInstance().changeUserName(u.getId(), userNameText.getText().toString());
			}
		});
	    
	    okAccesIdButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				EditText userAccessIdText = (EditText)findViewById(R.id.userAccessIdText);
				
				Controller.getInstance().changeUserAccessId(u.getId(), userAccessIdText.getText().toString());
			}
		});
	    
	    Button deleteButton = (Button)findViewById(R.id.deleteUserButton);
	    
	    deleteButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Controller.getInstance().deleteUser(u.getId());
			}
		});
	}

	public void backEditUser(View v) {
		
	}
	
	public void setDoorsUserAccess(boolean success) {
		if(success){
			
			//Show a notification if successful or unsuccessful.
		}
		else {
			
		}
	}
	
	public void setDoorsAndAccess() {
		
		ListView doorAccessListView = (ListView)findViewById(R.id.doorAccessListView);
		
		@SuppressWarnings("unchecked")
		ArrayAdapter<Door> adap = (ArrayAdapter<Door>)doorAccessListView.getAdapter();
		
		adap.clear();
		
		for(Door d : Controller.getInstance().userAccessMap.keySet()) {
			adap.add(d);
		}
		
		int count = adap.getCount();
		
	    for (int j = 0; j < count; j++){
	        	
	    	if (Controller.getInstance().userAccessMap.get(Controller.getInstance().doorList.get(j)) == true) {
			    doorAccessListView.setItemChecked(j, true);
			}
	    }
	    
	    adap.notifyDataSetChanged();
	}
	
	public void setDeleteNewUser(boolean response) {
		if (response) {
			Intent i = new Intent(this, MainActivity.class);
			startActivity(i);
		}
	}
}
